'******************************************************
'******************************************************
'Application Title: Google Widget Editor
'version: 1.0
'Date: 01-02-2008
'Requirements: win2k or later
'Author: Andrew Pye
'Author Email: StretchMasterP@googlemail.com
'License: Released under GNU GENERAL PUBLIC LICENSE v3
'ToDo:
'Changes:Imports System.Text
'******************************************************
'******************************************************

Namespace MPTools
    Namespace Strings

#Region " TagExtractor "
        ''' <summary>
        ''' Tag Extractor
        ''' </summary>
        ''' <remarks></remarks>
        Public Class TagExtractor
            Public Function ExtractTags(ByVal DataStr As String, ByVal StartTag As String, ByVal EndTag As String, Optional ByVal ReturnNameOnly As Boolean = False) As String()
                Dim SPos As Integer = 0
                Dim EPos As Integer = -1
                Dim TArray() As String = {}
                Dim TCount As Integer = 0
                ' Dim TArray As New ObjectModel.Collection(Of String)

                Do While SPos > -1
                    'loop through tag extracting tags
                    SPos = DataStr.IndexOf(StartTag, SPos)
                    If SPos > -1 Then
                        EPos = DataStr.IndexOf(EndTag, SPos + 1)
                        If EPos > -1 Then
                            'extract the data with tags
                            If ReturnNameOnly = True Then
                                TCount = TCount + 1
                                Array.Resize(TArray, TCount)
                                TArray(TCount - 1) = StringTags(DataStr.Substring(SPos, (EPos + (EndTag.Length)) - SPos), StartTag, EndTag)
                                ' TArray.Add(StringTags(DataStr.Substring(SPos, (EPos + (EndTag.Length)) - SPos), StartTag, EndTag))
                            Else
                                TCount = TCount + 1
                                Array.Resize(TArray, TCount)
                                If (EPos + (EndTag.Length)) - SPos >= DataStr.Length Then
                                    Exit Do
                                Else
                                    TArray(TCount - 1) = DataStr.Substring(SPos, (EPos + (EndTag.Length)) - SPos)
                                End If
                                'TArray.Add(DataStr.Substring(SPos, (EPos + (EndTag.Length)) - SPos))
                            End If
                            SPos = (EPos + (EndTag.Length))
                        End If
                    End If
                Loop
                Return TArray
            End Function
            Public Function StringTags(ByVal DataStr As String, ByVal StartTag As String, ByVal EndTag As String) As String
                Dim Tstr As String = DataStr
                Tstr = Tstr.Replace(StartTag, "")
                Tstr = Tstr.Replace(EndTag, "")
                Return Tstr
            End Function
        End Class
#End Region

        Public Class StringData
            'strips nukbers from end of a string
            Public Shared Function RemoveNumbersFromEnd(ByVal Value As String) As String
                Dim TValue As String = Value
                For i As Integer = Value.Length - 1 To 0 Step -1
                    If IsNumeric(Value.Substring(i, 1)) = False Then
                        Return Value.Substring(0, i + 1)
                    End If
                Next
                Return Value
            End Function
            'strips numbers fro the start of a string
            Public Shared Function RemoveNumbersFromStart(ByVal Value As String) As String
                Dim TValue As String = Value
                For i As Integer = 0 To Value.Length - 1
                    If IsNumeric(Value.Substring(i, 1)) = False Then
                        Return Value.Substring(i, Value.Length - i)
                    End If
                Next
                Return Value
            End Function
            'strips numbers from the string
            Public Shared Function RemoveNumbers(ByVal Value As String) As String
                Dim TValue As String = ""
                For i As Integer = 0 To Value.Length - 1
                    If IsNumeric(Value.Substring(i, 1)) = False Then
                        TValue = TValue & Value.Substring(i, 1)
                    End If
                Next
                Return Value
            End Function
            Public Shared Function SubstringStartEnd(ByVal Value As String, ByVal StartPos As String, ByVal EndPos As String) As String
                If StartPos < EndPos Then
                    Dim TValue As String = ""
                    TValue = Value.Substring(StartPos, Value.Length - EndPos)
                    Return TValue
                End If
                Return Value
            End Function
        End Class


        Public Class TextData
            Private Function CalcBestFitFontSize(ByVal Canvas As System.Drawing.Graphics, ByVal TText As String, ByVal TFont As System.Drawing.Font, ByVal TWidth As Integer, ByVal THeight As Integer) As System.Drawing.Font
                Dim g As System.Drawing.Graphics = Canvas
                Dim w As System.Drawing.SizeF = g.MeasureString(TText, TFont)
                Dim MyFont As System.Drawing.Font = TFont
                MyFont = New System.Drawing.Font(TFont.FontFamily, TFont.Size * CSng(TWidth / w.Width), TFont.Style, TFont.Unit)
                w = g.MeasureString(TText, MyFont)
                If w.Height > THeight Then
                    MyFont = New System.Drawing.Font(TFont.FontFamily, (TFont.Size * CSng(THeight / w.Height)) + (THeight * 0.4), TFont.Style, TFont.Unit)
                ElseIf w.Width > TWidth Then
                    MyFont = New System.Drawing.Font(MyFont.FontFamily, MyFont.Size - 1, MyFont.Style, MyFont.Unit)
                End If
                Return MyFont
            End Function
        End Class



    End Namespace
End Namespace